﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Homework_5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] color = { "black", "blue", "yellow", "green", "red","gray"};
            foreach (string co in color)
            {
                comboBox1.Items.Add(co);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (graphics==null)
            {
                graphics = this.CreateGraphics();
            }
            drawCayleyTree(10, 400, 310, 100, -Math.PI / 2);
        }

        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;

        void drawCayleyTree(int n,double x0,double y0,double leng,double th)
        {
            if (n==0)
            {
                return;
            }
            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0,double y0,double  x1,double y1)
        {
            graphics.DrawLine(
                Pens.Blue,
                (int)x0, (int)y0, (int)x1, (int)y1);
        }

        //调参后的新树
        private Graphics ngraphics;
        double nth1 = 30 * Math.PI / 180;
        double nth2 = 20 * Math.PI / 180;
        double nper1 = 0.6;
        double nper2 = 0.7;
        int nn = 10;
        double nleng = 100;
        Pen pen;

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text!="")
            {
                nn = int.Parse(textBox1.Text);
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                nleng = double.Parse(textBox2.Text);
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (textBox3.Text!="")
            {
                nper1 = double.Parse(textBox3.Text);
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (textBox4.Text!="")
            {
                nper2 = double.Parse(textBox4.Text);
            }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if (textBox5.Text!="")
            {
                nth1 = double.Parse(textBox5.Text) * Math.PI / 180;
            }
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            if (textBox6.Text!="")
            {
                nth2 = double.Parse(textBox6.Text) * Math.PI / 180;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
                switch (comboBox1.SelectedIndex)
                {
                    case 0:pen = Pens.Black;
                        break;
                    case 1:pen = Pens.Blue;
                        break;
                    case 2:pen = Pens.Yellow;
                        break;
                    case 3:pen = Pens.Green;
                        break;
                    case 4:pen = Pens.Red;
                        break;
                    case 5:pen = Pens.Gray;
                        break;
                    default:
                        break;
                }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (ngraphics == null)
                {
                    graphics = this.CreateGraphics();
                }
                if (ngraphics == null)
                {
                    ngraphics = this.CreateGraphics();
                }
                graphics.Clear(Color.White);
                ngraphics.Clear(Color.White);
                label9.Text = "";
                ndrawCayleyTree(nn, 400, 310, nleng, -Math.PI / 2);
            }
            catch (Exception)
            {

                label9.Text = "请先输入参数";
            }
            
        }

        void ndrawCayleyTree(int n, double x0, double y0, double leng, double th)
        {
            if (n == 0)
            {
                return;
            }
            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            ndrawLine(x0, y0, x1, y1);

            ndrawCayleyTree(n - 1, x1, y1, nper1 * leng, th + nth1);
            ndrawCayleyTree(n - 1, x1, y1, nper2 * leng, th - nth2);
        }
        void ndrawLine(double x0, double y0, double x1, double y1)
        {
            ngraphics.DrawLine(
               pen,
                (int)x0, (int)y0, (int)x1, (int)y1);
        }
    }
}
